Reflection .NET API
Programming Concepts / Handling Events
In This Topic
    Handling Events
    In This Topic

    Events are an integral part of the API, and provide an asynchronous way to handle state changes in Reflection. By implementing event handler methods, you can receive notifications for all event-generating activity, such as keyboard input and host actions. In many cases, you can modify or cancel the activity that caused the event.

    For examples of using Reflection events, see:

    Dynamically Change the User Interface

    Monitor Credit Card Access

    Log User Input

    Save Screens As Text Or Images

    Logging Events

    The Event Logger program demonstrates the range of events in Reflection to which you can add event handling functions and the order in which those events occur. This program sets up each event with an event handler that prompts a text notification immediately after the event occurs.

    To set up the Event Logger program

                   
    1. In Visual Studio, create a new Console Application project and add references for the following Reflection assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
      Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.IbmHosts                   
      Attachmate.Reflection.UserInterface
    2. Replace all the code in the Program.cs file with the following code for the terminal you are using.  Then change the IP address and port to match those for your host and run the program.

      Log events
      Copy Code
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection.Productivity;
      namespace EventLogger
      {
          class Program
          {
              static void Main(string[] args)
              {
      
                  //Start a visible instance of Reflection or get the instance running at the given channel name
                  Application app = MyReflection.CreateApplication("myWorkspace", true);
      
                  //Create and configure a terminal
                  IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}"));              
                  terminal.HostAddress = "demo:ibm3270.sim";
                  terminal.Port = 623;
      
                  //Get the Reflection objects to log events for
                  IIbmScreen screen = terminal.Screen;
                  IFrame frame = (IFrame)app.GetObject("Frame");
                  IMacro macro = terminal.Macro;
                  IOia rOia = screen.Oia;
                  ISpellCheck rSpell = terminal.Productivity.SpellCheck;
                  IRecentTyping rTyping = terminal.Productivity.RecentTyping;
                  IAutoComplete rAutoComp = terminal.Productivity.AutoComplete;
                  IAutoExpand rAutoExp = terminal.Productivity.AutoExpand;
      
                  //Set up event handler for IFrame event before creating a new view.
                  frame.ViewOpened += new ViewEventHandler(frame_ViewOpened);
      
                  //Create a new view to make the terminal visible in the MDI frame.
                  IView view = frame.CreateView(terminal);
                     
                  //Create event handlers
                  view.Closed += new EventHandler(view_Closed);
                  view.Closing += new CancelableEventHandler(view_Closing);
                  view.Deselected += new EventHandler(view_Deselected);
                  view.Selected += new EventHandler(view_Selected);
                  view.TitleTextChanged += new EventHandler(view_TitleTextChanged);
                  terminal.AfterConnect += new EventHandler(terminal_AfterConnect);
                  terminal.AfterDisconnect += new EventHandler(terminal_AfterDisconnect);
                  terminal.BeforeConnect += new EventHandler(terminal_BeforeConnect);
                  terminal.BeforeDisconnect += new EventHandler(terminal_BeforeDisconnect);
                  terminal.Closed += new EventHandler(terminal_Closed);
                  screen.AfterSendKeys += new AfterSendKeysEventHandler(screen_AfterSendKeys);
                  screen.BeforeSendControlKey += new BeforeSendControlKeyEventHandler(screen_BeforeSendControlKey);
                  screen.BeforeSendKeys += new BeforeSendKeysEventHandler(screen_BeforeSendKeys);
                  screen.CursorInNewField += new CursorInNewFieldEventHandler(screen_CursorInNewField);
                  screen.KeyboardLocked += new EventHandler(screen_KeyboardLocked);
                  screen.KeyboardUnlocked += new EventHandler(screen_KeyboardUnlocked);
                  screen.NewScreenReady += new EventHandler(screen_NewScreenReady);
                  screen.ScreenChanged += new EventHandler(screen_ScreenChanged);
                  macro.MacroCompleted += new EventHandler(macro_MacroCompleted);
                  macro.MacroStarted += new EventHandler(macro_MacroStarted);
                  rOia.OiaChanged += new EventHandler(rOia_OiaChanged);
                  rSpell.MisspelledWord += new Attachmate.Reflection.Productivity.MisspelledWordEventHandler(rSpell_MisspelledWord);
                  rTyping.ListChanged += new EventHandler(rTyping_ListChanged);
                  rAutoComp.DictionaryChanged += new EventHandler(rAutoComp_DictionaryChanged);
                  rAutoComp.SuggestionAccepted += new Attachmate.Reflection.Productivity.AutoCompleteAcceptedEventHandler(rAutoComp_SuggestionAccepted);
                  rAutoExp.AutoExpanded += new Attachmate.Reflection.Productivity.AutoExpandEventHandler(rAutoExp_AutoExpanded);
                  rAutoExp.DictionaryChanged += new EventHandler(rAutoExp_DictionaryChanged);
                  //Connect and wait for the host
                  terminal.Connect();
                  screen.WaitForHostSettle(10000, 1000);
      
                  //You must connect to a session before retrieving an IScreenHistory object or a NullPointerException will occur.
                  IScreenHistory rScrHist = terminal.Productivity.ScreenHistory;
                  rScrHist.ScreenChanged += new EventHandler(rScrHist_ScreenChanged);
                  rScrHist.ScreenSelected += new EventHandler(rScrHist_ScreenSelected);
      
                  //Wait for input so that events are logged before exiting.
                  Console.Write("Press any key to exit EventsDemo...\n");
                  ConsoleKeyInfo cki = Console.ReadKey();
              }
      
              //Set up the event handlers to write messages to the console
              static void rAutoExp_DictionaryChanged(object sender, EventArgs e)
              {
                  Console.Write("rAutoExp_DictionaryChanged\n");
              }
              static void rAutoExp_AutoExpanded(object sender, Attachmate.Reflection.Productivity.AutoExpandEventArgs args)
              {
                  Console.Write("rAutoExp_AutoExpanded\n");
              }
              static void rAutoComp_SuggestionAccepted(object sender, Attachmate.Reflection.Productivity.AutoCompleteAcceptedEventArgs e)
              {
                  Console.Write("rAutoComp_SuggestionAccepted\n");
              }
              static void rAutoComp_DictionaryChanged(object sender, EventArgs e)
              {
                  Console.Write("rAutoComp_DictionaryChanged\n");
              }
              static void rTyping_ListChanged(object sender, EventArgs e)
              {
                  Console.Write("rTyping_ListChanged\n");
              }
              static void rScrHist_ScreenSelected(object sender, EventArgs e)
              {
                  Console.Write("rScrHist_ScreenSelected\n");
              }
              static void rScrHist_ScreenChanged(object sender, EventArgs e)
              {
                  Console.Write("rScrHist_ScreenChanged\n");
              }
              static void rSpell_MisspelledWord(object sender, Attachmate.Reflection.Productivity.MisspelledWordEventArgs args)
              {
                  Console.Write("rSpell_MisspelledWord\n");
              }
              static void rOia_OiaChanged(object sender, EventArgs e)
              {
                  Console.Write("rOia_OiaChanged\n");
              }
              static void macro_MacroStarted(object sender, EventArgs e)
              {
                  Console.Write("rMacro_MacroStarted\n");
              }
              static void macro_MacroCompleted(object sender, EventArgs e)
              {
                  Console.Write("rMacro_MacroCompleted\n");
              }
              static void screen_ScreenChanged(object sender, EventArgs e)
              {
                  Console.Write("rScreen_ScreenChanged\n");
              }
              static void screen_NewScreenReady(object sender, EventArgs e)
              {
                  Console.Write("rScreen_NewScreenReady\n");
              }
              static void screen_KeyboardUnlocked(object sender, EventArgs e)
              {
                  Console.Write("rScreen_KeyboardUnlocked\n");
              }
              static void screen_KeyboardLocked(object sender, EventArgs e)
              {
                  Console.Write("rScreen_KeyboardLocked\n");
              }
              static void screen_CursorInNewField(object sender, CursorInNewFieldEventArgs args)
              {
                  Console.Write("rScreen_CursorInNewField\n");
              }
              static void screen_BeforeSendKeys(object sender, BeforeSendKeysEventArgs args)
              {
                  Console.Write("rScreen_BeforeSendKeys\n");
              }
              static void screen_BeforeSendControlKey(object sender, BeforeSendControlKeyEventArgs args)
              {
                  Console.Write("rScreen_BeforeSendControlKey\n");
              }
              static void screen_AfterSendKeys(object sender, AfterSendKeysEventArgs args)
              {
                  Console.Write("rScreen_AfterSendKeys\n");
              }
              static void terminal_Closed(object sender, EventArgs e)
              {
                  Console.Write("rTerm_Closed\n");
              }
              static void terminal_BeforeDisconnect(object sender, EventArgs e)
              {
                  Console.Write("rTerm_BeforeDisconnect\n");
              }
              static void terminal_BeforeConnect(object sender, EventArgs e)
              {
                  Console.Write("rTerm_BeforeConnect\n");
              }
              static void terminal_AfterDisconnect(object sender, EventArgs e)
              {
                  Console.Write("rTerm_AfterDisconnect\n");
              }
              static void terminal_AfterConnect(object sender, EventArgs e)
              {
                  Console.Write("rTerm_AfterConnect\n");
              }
              static void view_TitleTextChanged(object sender, EventArgs e)
              {
                  Console.Write("rView_TitleTextChanged\n");
              }
              static void view_Selected(object sender, EventArgs e)
              {
                  Console.Write("rView_Selected\n");
              }
              static void view_Deselected(object sender, EventArgs e)
              {
                  Console.Write("rView_Deselected\n");
              }
              static void view_Closing(object sender, CancelableEventArgs args)
              {
                  Console.Write("rView_Closing\n");
              }
              static void view_Closed(object sender, EventArgs e)
              {
                  Console.Write("rView_Closed\n");
              }
              static void frame_ViewOpened(object sender, EventArgs e)
              {
                  Console.Write("rFrame_ViewOpened\n");
              }
          }
      }
                                         
      
         

     

                   
    1. In Visual Studio, create a new Console Application project and add references for the following Reflection assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
      Attachmate.Reflection.Framework 
      Attachmate.Reflection.Emulation.OpenSystems
      Attachmate.Reflection.UserInterface
    2. Replace all the code in the Program.cs file with the following code for the terminal you are using. Then change the IP address and port to match those for your host and run the program.

      Log events
      Copy Code
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.OpenSystems;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection.Productivity;
      namespace EventLogger
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //Start a visible instance of Reflection or get the instance running at the given channel name
                  Application app = MyReflection.CreateApplication("myWorkspace", true);
                  ITerminal terminal = (ITerminal)app.CreateControl(new Guid("{BE835A80-CAB2-40d2-AFC0-6848E486BF58}"));
                     
                  //Change the IP address and port to match those for your host.
                  IConnectionSettingsTelnet conn = (IConnectionSettingsTelnet)terminal.ConnectionSettings;
                  conn.HostAddress = "Sylvester";
                  IScreen screen = terminal.Screen;
                  IFrame frame = (IFrame)app.GetObject("Frame");
                  IMacro macro = terminal.Macro;
      
                  //Set up event handler for IFrame event before creating a new view.
                  frame.ViewOpened += new ViewEventHandler(frame_ViewOpened);
      
                  //Create a new view to make the terminal visible in the frame.
                  IView view = frame.CreateView(terminal);
      
                  //Create event handlers.
                  view.Closed += new EventHandler(view_Closed);
                  view.Closing += new CancelableEventHandler(view_Closing);
                  view.Deselected += new EventHandler(view_Deselected);
                  view.Selected += new EventHandler(view_Selected);
                  view.TitleTextChanged += new EventHandler(view_TitleTextChanged);
                  terminal.Connected += new ConnectionEventHandler(terminal_Connected);
                  terminal.Disconnected += new ConnectionEventHandler(terminal_AfterDisconnect);
                  terminal.Connecting += new CancelableConnectionEventHandler(terminal_BeforeConnect);
                  terminal.UserDisconnecting += new CancelableConnectionEventHandler(terminal_BeforeDisconnect);
                  terminal.Closed += new EventHandler(terminal_Closed);
                  screen.KeysSent += new KeysSentEventHandler(screen_KeysSent);
                  screen.KeysSending += new KeysSendingEventHandler(screen_BeforeSendKeys);
                  screen.ControlKeySending += new ControlKeySendingEventHandler(screen_ControlKeySending);
                  screen.KeyboardLocked += new EventHandler(screen_KeyboardLocked);
                  screen.KeyboardUnlocked += new EventHandler(screen_KeyboardUnlocked);
                  macro.MacroCompleted += new EventHandler(macro_MacroCompleted);
                  macro.MacroStarted += new EventHandler(macro_MacroStarted);
                  //Connect and wait for the host to be ready
                  terminal.Connect();
                  screen.WaitForHostSettle(10000, 1000);
                  
                  //You must connect to a session before retrieving an IScreenHistory object or a NullPointerException will occur.
                  IScreenHistory rScrHist = terminal.Productivity.ScreenHistory;
                  rScrHist.ScreenChanged += new EventHandler(rScrHist_ScreenChanged);
                  rScrHist.ScreenSelected += new EventHandler(rScrHist_ScreenSelected);
      
                  //Wait for input so that events are logged before exiting.
                  Console.Write("Press any key to exit EventsDemo...\n");
                  ConsoleKeyInfo cki = Console.ReadKey();
              }
              //Set up the event handlers to write messages to the console
              static void screen_BeforeSendKeys(object sender, KeysSendingEventArgs e)
              {
                  Console.Write("rScreen_BeforeSendKeysChanged\n");
              }
              static void screen_KeysSent(object sender, KeysSentEventArgs e)
              {
                  Console.Write("rScreen_KeysSentChanged\n");
              }
              static void screen_ControlKeySending(object sender, ControlKeySendingEventArgs e)
              {
                  Console.Write("rScreen_ControlKeySendingChanged\n");
              }
              static void rAutoExp_DictionaryChanged(object sender, EventArgs e)
              {
                  Console.Write("rAutoExp_DictionaryChanged\n");
              }
              static void rAutoExp_AutoExpanded(object sender, Attachmate.Reflection.Productivity.AutoExpandEventArgs args)
              {
                  Console.Write("rAutoExp_AutoExpanded\n");
              }
              static void rAutoComp_SuggestionAccepted(object sender, Attachmate.Reflection.Productivity.AutoCompleteAcceptedEventArgs e)
              {
                  Console.Write("rAutoComp_SuggestionAccepted\n");
              }
              static void rAutoComp_DictionaryChanged(object sender, EventArgs e)
              {
                  Console.Write("rAutoComp_DictionaryChanged\n");
              }
              static void rTyping_ListChanged(object sender, EventArgs e)
              {
                  Console.Write("rTyping_ListChanged\n");
              }
              static void rScrHist_ScreenSelected(object sender, EventArgs e)
              {
                  Console.Write("rScrHist_ScreenSelected\n");
              }
              static void rScrHist_ScreenChanged(object sender, EventArgs e)
              {
                  Console.Write("rScrHist_ScreenChanged\n");
              }
              static void rSpell_MisspelledWord(object sender, Attachmate.Reflection.Productivity.MisspelledWordEventArgs args)
              {
                  Console.Write("rSpell_MisspelledWord\n");
              }
              static void rOia_OiaChanged(object sender, EventArgs e)
              {
                  Console.Write("rOia_OiaChanged\n");
              }
              static void macro_MacroStarted(object sender, EventArgs e)
              {
                  Console.Write("rMacro_MacroStarted\n");
              }
              static void macro_MacroCompleted(object sender, EventArgs e)
              {
                  Console.Write("rMacro_MacroCompleted\n");
              }
              static void rScreen_ScreenChanged(object sender, EventArgs e)
              {
                  Console.Write("rScreen_ScreenChanged\n");
              }
              static void rScreen_NewScreenReady(object sender, EventArgs e)
              {
                  Console.Write("rScreen_NewScreenReady\n");
              }
              static void screen_KeyboardUnlocked(object sender, EventArgs e)
              {
                  Console.Write("rScreen_KeyboardUnlocked\n");
              }
              static void screen_KeyboardLocked(object sender, EventArgs e)
              {
                  Console.Write("rScreen_KeyboardLocked\n");
              }
      
              static void terminal_Closed(object sender, EventArgs e)
              {
                  Console.Write("rTerm_Closed\n");
              }
              static void terminal_BeforeDisconnect(object sender, CancelableConnectionEventArgs e)
              {
                  Console.Write("rTerm_BeforeDisconnect\n");
              }
              static void terminal_BeforeConnect(object sender, CancelableConnectionEventArgs e)
              {
                  Console.Write("rTerm_BeforeConnect\n");
              }
              static void terminal_AfterDisconnect(object sender, ConnectionEventArgs e)
              {
                  Console.Write("rTerm_AfterDisconnect\n");
              }
              static void terminal_Connected(object sender, ConnectionEventArgs e)
              {
                  Console.Write("rTerm_AfterConnect\n");
              }
              static void view_TitleTextChanged(object sender, EventArgs e)
              {
                  Console.Write("rView_TitleTextChanged\n");
              }
              static void view_Selected(object sender, EventArgs e)
              {
                  Console.Write("rView_Selected\n");
              }
              static void view_Deselected(object sender, EventArgs e)
              {
                  Console.Write("rView_Deselected\n");
              }
              static void view_Closing(object sender, CancelableEventArgs args)
              {
                  Console.Write("rView_Closing\n");
              }
              static void view_Closed(object sender, EventArgs e)
              {
                  Console.Write("rView_Closed\n");
              }
              static void frame_ViewOpened(object sender, EventArgs e)
              {
                  Console.Write("rFrame_ViewOpened\n");
              }
          }
      }